package com.red.wine.ui;

import java.io.IOException;

import com.baidu.location.i.b;
import com.google.json.Gson;
import com.google.json.JsonParseException;
import com.red.wine.R;
import com.red.wine.WineApp;
import com.red.wine.model.FeedData;
import com.red.wine.model.FeedReq;
import com.red.wine.model.Headers;
import com.red.wine.model.RegisterData;
import com.red.wine.model.RegisterReq;
import com.red.wine.model.RegisterReturn;
import com.red.wine.util.IntentUtil;
import com.red.wine.util.PrefsUtils;
import com.red.wine.util.StringUtil;
import com.red.wine.webservice.ServiceAsynTask;
import com.red.wine.webservice.ServiceHelper;
import com.red.wine.webservice.ServiceHost;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class FeedBackActivity extends BaseActivity {
	ImageView back;
	Context context;
	EditText editText;
	Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.return_info_layout);
		context = this;
		back = (ImageView) findViewById(R.id.return_info_back);
		back.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				IntentUtil.redirectToNext(context, MyCenterActivity.class);
				finish();
			}
		});
		editText = (EditText) findViewById(R.id.yijian_edit);
		button = (Button) findViewById(R.id.fankui_btn);
		button.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (WineApp.getInstance().isWiFiActive() || WineApp.getInstance().isNetworkAvailable()) {
					Log.e("TAG", "register: = " + PrefsUtils.getHaseRegister());
					if(!StringUtil.isEmptyOrNull(PrefsUtils.getHaseRegister())){
						String content = editText.getText().toString().trim();
						if (!StringUtil.isEmptyOrNull(content)) {
							getData(content);
						} else {
							Toast.makeText(context, "反馈内容不能为空", Toast.LENGTH_SHORT)
									.show();
						}
					}else{
						PrefsUtils.setName(null);
						PrefsUtils.setPass(null);
						Toast.makeText(context, "请先登录", Toast.LENGTH_SHORT).show();
						IntentUtil.redirectToNext(context, LoginActivity.class);
						finish();
					}
				} else {
					Toast.makeText(context, "无可用网络", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	private void getData(final String Content) {
		showLoading();
		new ServiceAsynTask<RegisterReturn>() {

			@Override
			protected RegisterReturn callService() throws IOException,
					JsonParseException, AssertionError {
				FeedReq taste = new FeedReq();
				Headers header = new Headers();
				header.setUa("ANDROID_HY/2.0.1.0 (customerid=gypsii, variant=gypsii)");

				FeedData data = new FeedData();
				data.setContent(Content);

				taste.cmd = "user_feedback";
				taste.headers = header;
				taste.data = data;
				taste.sid = "7b8bb3e0152c9ed086f287ac37b927aa9085f2";

				String up = new Gson().toJson(taste);
				String send = StringUtil.unicodeToUtf8("json=" + up);
				String urlstr = ServiceHost.getTasteURL();

				return ServiceHelper.getInstance().postData(urlstr, send,
						RegisterReturn.class);
			}

			@Override
			protected void runWithResult(RegisterReturn result)
					throws Exception {

				if (result != null) {
					bindData(result);
					Log.e("TAG", "isnotnull");
				} else {
					// Log.e("TAG", "is null");
					Toast.makeText(context, "反馈失败", Toast.LENGTH_SHORT).show();
				}
				hideLoading();
			}

			@Override
			protected void runWithError(RegisterReturn result, int iErrorCode)
					throws Exception {
				Log.e("TAG", "异常");
				IntentUtil.redirectToNext(context, LoginActivity.class);
				finish();
				hideLoading();
			}
		}.execute();
	}

	private void bindData(RegisterReturn result) {
		if (result.getRsp().equals("1")) {
			Toast.makeText(context, "反馈成功", Toast.LENGTH_SHORT).show();
			IntentUtil.redirectToNext(context, MyCenterActivity.class);
			finish();
		} else {
			Toast.makeText(context, "反馈失败", Toast.LENGTH_SHORT).show();
		}
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			IntentUtil.redirectToNext(context, MyCenterActivity.class);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
